import javax.swing.*
import net.sshiffon.*
import net.miginfocom.swing.MigLayout
import	java.awt.*
import	java.awt.event.*

actions {
	action(id: "retryAction",
			name: "Retry",
			enabled: true,
			closure: {
				loadingLabel.visible=true
				retryButton.visible= false
				doLater {
					controller.open()
				}
            })
	action(id: "terminalAction",
			//name: "Terminal",
			mnemonic: "T",
			enabled: false,
//			accelerator: shortcut("T"),
			closure: {
				//logger.debug('ok')
				controller.initialiseShell()
				mainPanel.layout.show( view.mainPanel, "terminalCard" )
				controller.setDisableOnly('Terminal')
				doLater {
					model.termGroup?.view?.terminalPanel?.requestFocusInWindow()
				}
            })
	action(id: "sftpAction",
			//name: "sFtp",
			mnemonic: "F",
			enabled: false,
			closure: {
				controller.initialiseSftp()
				mainPanel.layout.show( view.mainPanel, "sftpCard" )
				controller.setDisableOnly('Sftp')
            })
	action(id: "configAction",
			//name: "Config",
			mnemonic: "C",
			enabled: false,
//			accelerator: shortcut("C"),
			closure: {
				mainPanel.layout.show( view.mainPanel, "configCard" )
				controller.setDisableOnly('Config')
            })
    action(id: "quitAction",
			name: "Close",
			closure: {
			    app.controllers.Sshiffon.closeAction()
            })            
}
panel(id: 'rootPanel', layout: new MigLayout("fill, ins 0")) {
	
	panel(id: 'mainPanel', constraints: 'grow', layout: new CardLayout()) 
	{

	    panel(id:'connectingCard', constraints: "connectingCard",layout: new MigLayout("fill"), background: Color.WHITE)
		{
	            panel(constraints:"center", background: Color.WHITE,layout: new MigLayout()) {
	        		label("Connecting to ${model.savedName}",id:"statusMessage", constraints: 'center,wrap',font: rootPanel.font.deriveFont(16.0f))
		            label(icon:imageIcon('/page_loading_ani.gif'), constraints: 'center,wrap', id:'loadingLabel')
		            panel(background: Color.WHITE) {
	        			button(action: retryAction, visible: false, constraints: '', id:'retryButton')
	        			button(action: quitAction, visible: false, constraints: 'wrap', id:'quitButton')
	        		}
	            }
	    }	
	    
	    panel(constraints:'terminalCard', id:'terminalCard', background: bind { model.terminalBackground }, layout: new MigLayout("fill, ins 0")) //tabToolTip:'Terminal',tabIcon: imageIcon('/crystal24/terminal.png'))
		{
			    }.addMouseListener( { controller.initialiseShell() } as java.awt.event.MouseListener);
	    panel(constraints:'sftpCard', id:'sftpCard', layout: new MigLayout("fill, ins 0")) //tabToolTip:'File Transfer', tabIcon:  imageIcon('/crystal24/file-manager.png')) 
		{
			        splitPane(id:'sftpSplitPane', constraints: "grow", dividerLocation: 350) 
			        {
			        	panel(id: "localCards", layout: new CardLayout(), constraints: "left") 
			        	{
			        		panel( constraints: "loading",layout: new MigLayout("fill"), background: Color.WHITE)
			        		{
			        			label(' Loading local files ...', constraints:'north')
			        			label(icon:imageIcon('/page_loading_ani.gif'), constraints: 'center')
			        		}
			        		panel( id: 'localViewHolderPanel', constraints:"loaded",layout: new MigLayout("fill, ins 0")) 
			        		{
			        			//local view goes here
			        		}
			        		
			        	}
			        	panel(id: "remoteCards", layout: new CardLayout(), constraints: "right") 
			        	{
			        		panel( constraints: "loading",layout: new MigLayout("fill"), background: Color.WHITE)
			        		{
			        			label(' Loading remote files ...', constraints:'north')
			        			label(icon:imageIcon('/page_loading_ani.gif'), constraints: 'center')
			        		}
			        		panel( id: 'remoteViewHolderPanel', constraints:"loaded",layout: new MigLayout("fill, ins 0") ) 
			        		{
			        			//remote view goes here
			        		}
			        	}
			        	
				        } // local & remote panes load here
			    }.addMouseListener( { controller.initialiseSftp() } as java.awt.event.MouseListener)
			}.addKeyListener( { evt ->
					if('Terminal'.equals(model.selectedCard))
					{
		                char keyChar = evt.getKeyChar();
		                if( !Character.isISOControl(keyChar) && evt.getID()==KeyEvent.KEY_RELEASED)
		                {
		                    // send character & focus to terminal
		                    logger.debug(".. tabbedPane attempting to forward character ${keyChar} to terminal ..")
		                    model.termGroup.view.terminalPanel.requestFocusInWindow()
		                    byte[] b= new byte[1]
		                    b[0]= (byte) keyChar
		                    model.termGroup.view.terminalPanel.emulator.sendBytes(b)
		                }
					}
                } as java.awt.event.KeyListener )
			terminalCard.addFocusListener( { evt ->
			    	logger.debug('terminal card got focus')
			    	model.termGroup.view.terminalPanel.requestFocusInWindow()
			    } as java.awt.event.FocusListener );

		
}

